package cn.com.tianpeidong.restaurantadmin.project.admin.menu.service.impl;

import cn.com.tianpeidong.restaurantadmin.common.exception.BusinessException;
import cn.com.tianpeidong.restaurantadmin.common.util.msg.MessageUtils;
import cn.com.tianpeidong.restaurantadmin.common.util.string.StringUtils;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.entity.MenuInfo;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.entity.Permission;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.mapper.MenuMapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.mapper.PermissionMapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.service.IMenuService;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.vo.MenuVO;
import cn.com.tianpeidong.restaurantadmin.project.admin.role.entity.RolePermission;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.com.tianpeidong.restaurantadmin.common.web.constant.ProjectConstant.*;

/**
 * @author tianpeidong
 * @version 创建时间: 19-4-7 下午6:13
 */
@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<MenuInfo> listMenu(@NonNull MenuInfo menuInfo) {
        return menuMapper.list(menuInfo);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public MenuInfo add(@NonNull MenuInfo menuInfo) {
        String menuName = menuInfo.getMenuName();
        String menuCode = menuInfo.getMenuCode();
        if (StringUtils.isEmpty(menuName) || StringUtils.isEmpty(menuCode)) {
            throw new BusinessException(MessageUtils.message("menu.not.complete"));
        }
        MenuInfo db = menuMapper.getByMenuName(menuInfo.getMenuName());
        if (db != null) {
            throw new BusinessException(MessageUtils.message("menu.name.duplicate"));
        }
        MenuInfo codeExist = menuMapper.getByMenuCode(menuCode);
        if (codeExist != null) {
            throw new BusinessException(MessageUtils.message("menu.code.duplicate"));
        }
        int res = menuMapper.insert(menuInfo);
        if (res > 0) {
            List<Permission> permissionList = generatePermission(menuInfo);
            permissionMapper.initMenuPermission(permissionList);
//            List<RolePermission> rolePermissions = new ArrayList<>();
//            for (Permission perm : permissionList) {
//                RolePermission rolePermission = new RolePermission();
//                rolePermission.setRoleId(5L);
//                rolePermission.setPermissionId(perm.getId());
//                rolePermission.setVersion(1);
//                rolePermission.setCreatedTime(new Date());
//                rolePermission.setUpdatedTIme(new Date());
//                rolePermissions.add(rolePermission);
//            }
//            permissionMapper
        }

        return menuInfo;
    }

    private List<Permission> generatePermission(MenuInfo menuInfo) {
        List<Permission> list = new ArrayList<>();
        for (int i = 0; i < DEFAULT_PERMISSION_SIZE; i++) {
            Permission permission = new Permission();
            permission.setMenuId(menuInfo.getId());
            permission.setPermissionName(PERMISSION_MAP_CH.get(i) + menuInfo.getMenuName());
            permission.setPermissionCode(menuInfo.getMenuCode() + ":" + PERMISSION_MAP_EN.get(i));
            permission.setRemark(i == 0 ? "1" : "0");
            list.add(permission);
        }
        return list;
    }

    @Override
    public MenuInfo edit(@NonNull MenuInfo menuInfo) {
        MenuInfo db = menuMapper.getById(menuInfo.getId());
        if (db == null) {
            throw new BusinessException(MessageUtils.message("menu.not.exist"));
        }
        String menuName = menuInfo.getMenuName();
        String menuCode = menuInfo.getMenuCode();
        if (StringUtils.isEmpty(menuName) || StringUtils.isEmpty(menuCode)) {
            throw new BusinessException(MessageUtils.message("menu.not.complete"));
        }
        MenuInfo duplicateName = menuMapper.getByMenuName(menuInfo.getMenuName());
        if (duplicateName != null && ! duplicateName.getId().equals(db.getId())) {
            throw new BusinessException(MessageUtils.message("menu.name.duplicate"));
        }
        MenuInfo codeExist = menuMapper.getByMenuCode(menuCode);
        if (codeExist != null && ! duplicateName.getId().equals(db.getId())) {
            throw new BusinessException(MessageUtils.message("menu.code.duplicate"));
        }
        menuMapper.update(menuInfo);
        return menuInfo;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int delete(@NonNull Long menuId) {
        int count = permissionMapper.checkMenuUnderUse(menuId);
        if (count > 0) {
            throw new BusinessException(MessageUtils.message("menu.under.use"));
        }
        menuMapper.delete(menuId);
        permissionMapper.deleteByMenuId(menuId);
        return SUCCESS;
    }

    @Override
    public MenuInfo getMenuById(@NonNull Long id) {
        return menuMapper.getById(id);
    }

    @Override
    public List<MenuVO> listAllMenu() {
        return menuMapper.listAllMenu();
    }
}
